#include "ebm_env.h"
#include "ebm_msg.h"
#include "board.h"

#define PLAY_MANAGE_CNT 150

#define PLAY_CMD_IDLE 0
#define PLAY_CMD_PLAY 1
#define PLAY_CMD_STOP 2
#define PLAY_CMD_ADJ_VOL 3

struct ebm_play_sta{
	unsigned char play_manage_cmd;
	unsigned char play_manage_tcnt;
	unsigned char vol_current;
	unsigned char vol_expected;
	unsigned char iflag;
};

static struct ebm_play_sta pg_sta={0};

/*接口实现*/
void ebm_plat_media_open(void)
{
#define NSTAR_AUD_STREAM_TYPE_MP3 3
#define NSTAR_SOURCE_TYPE_ES 1

	//nstar_adt_socket_audio_config(NSTAR_AUD_STREAM_TYPE_MP3,NSTAR_SOURCE_TYPE_ES);
	mp3_media_open();
}

void ebm_plat_media_close(void)
{
	//nstar_adt_socket_play_stop();
	 mp3_media_close();
}

void ebm_trumpet_open(void)
{
//const char *cmd = "amixer sset 'Headphone',0 45% on";    
	HCF4051_OPEN_IP();
    //sys_popen((char*)cmd, 0);
}

void ebm_trumpet_close(void)
{
	pg_sta.vol_current= 0;
	HCF4051_CLOSE_IP();
}


unsigned char ebm_plat_mp3_play(unsigned char *data, unsigned int len)
{
	mp3_buf_write(data, len);
    return 0;
}

/*end 接口实现--------------------------------------*/




void player_volume_update(void)
{
#define _VOLUME_MAX 90
	unsigned char vol,vol_max;
	vol= visit_pg_vol();
	vol_max= ebm_env_get_volmax();
	if(vol_max > _VOLUME_MAX)
		vol_max= _VOLUME_MAX;
	if(vol <= 100){
		pg_sta.vol_expected= vol;
	}
	else if( 0 != visit_pg_eflg())
	{
		pg_sta.vol_expected= vol_max;
	}
	else{
		vol = ebm_env_get_vol();
		if(vol > vol_max)
			vol= vol_max;
		pg_sta.vol_expected= vol;
	}
}

void player_volume_adjust(void)
{
#define VOL_ADJ_STEP 6
	if(pg_sta.vol_current != pg_sta.vol_expected){
		int adj, new_val;
		adj= pg_sta.vol_expected- pg_sta.vol_current;
		if(adj > VOL_ADJ_STEP){
			adj= VOL_ADJ_STEP;
		}else if(adj < -VOL_ADJ_STEP){
			adj= -VOL_ADJ_STEP;
		}
		new_val= pg_sta.vol_current+ adj;
		if(new_val < 0)
			new_val=0;
		else if(new_val > 99)
			new_val=99;
		pg_sta.vol_current= new_val;
		ebmdbg_board("change vol (%d): %02d / %02d\n", adj, pg_sta.vol_current, pg_sta.vol_expected);
		board_cmm_setvol(new_val);
	}
}


void ebm_play_manage_loop(void)
{
	static volatile long play_manage_timer;
#define TCNT_TICK_SLEEP_MS(n) app_sleep(&play_manage_timer, n)
#define TCNT_TICK_PEND(n) app_pend_wake(play_manage_timer)

	static volatile long play_ctl_timer;
#define CTL_TICK_SLEEP_S(n) app_sleep(&play_ctl_timer, n*1000)
#define CTL_TICK_PEND(n) app_pend_wake(play_ctl_timer)

	unsigned char res= PLAY_CMD_IDLE;
	res= pg_sta.play_manage_cmd;
	if(res == PLAY_CMD_PLAY){
		pg_sta.play_manage_tcnt=PLAY_MANAGE_CNT;
		pg_sta.play_manage_cmd= PLAY_CMD_IDLE;
	}
	else if(res == PLAY_CMD_STOP){
		pg_sta.play_manage_tcnt= 0;
		pg_sta.play_manage_cmd= PLAY_CMD_IDLE;
	}
	else if(TCNT_TICK_PEND()){
		/* 没有直接指令，把心跳计数减1，并处理音量调节*/
		if(pg_sta.play_manage_tcnt > 0){
			res= PLAY_CMD_ADJ_VOL;
			pg_sta.play_manage_tcnt--;
		}
		TCNT_TICK_SLEEP_MS(200);
	}

	/*补充基于心跳数的判断逻辑*/
	if(CTL_TICK_PEND()){
		if(pg_sta.play_manage_tcnt > 0)
			res= PLAY_CMD_PLAY;
		else {
			res= PLAY_CMD_STOP;
		}
		CTL_TICK_SLEEP_S(6);
	}	
	
	switch(res)
	{
		case PLAY_CMD_PLAY:
			ebm_trumpet_open();
			break;
		case PLAY_CMD_STOP:
			ebm_trumpet_close();
			ebm_plat_media_close();
			current_pg_clear();
			rtp_net_stop();
			break;
		case PLAY_CMD_ADJ_VOL:
			player_volume_adjust();
			break;	
		default:
			break;
	}
}	





void ebm_play_cmd_open(void)
{
	pg_sta.play_manage_cmd= PLAY_CMD_PLAY;
}

void ebm_play_cmd_keepon(void)
{
	pg_sta.play_manage_tcnt=PLAY_MANAGE_CNT;
}

void ebm_play_cmd_stop(void)
{
	pg_sta.play_manage_cmd= PLAY_CMD_STOP;
}





